import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author YuanYuan
 * @Date 2022/11/26
 * @Time 11:17
 */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    //接受请求
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setContentType("application/json;charset=utf8");
//        Blog blog = objectMapper.readValue(req.getInputStream(),Blog.class);
//        try {
//            BlogDAO.insert(blog);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }
    //发送回应-显示博客列表
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");

        try {
            resp.getWriter().write(objectMapper.writeValueAsString(BlogDAO.selectAll()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
